import itertools
import random

def is_arithmetic(seq):
    """检查序列是否为等差数列"""
    if len(seq) < 3:
        return False
    diff = seq[1] - seq[0]
    for i in range(2, len(seq)):
        if seq[i] - seq[i-1] != diff:
            return False
    return True

def is_geometric(seq):
    """检查序列是否为等比数列"""
    if len(seq) < 3 or seq[0] == 0:
        return False
    ratio = seq[1] / seq[0]
    for i in range(2, len(seq)):
        if seq[i-1] == 0 or seq[i] / seq[i-1] != ratio:
            return False
    return True

def generate_numbers(base, upper_limit, max_attempts=1000):
    """
    生成4个个位为0的整数，使其与基数的差不构成等差或等比数列
    
    参数:
    base (int): 给定的基数
    upper_limit (int): 生成数值的上限（包含）
    max_attempts (int): 最大尝试次数
    
    返回:
    list: 满足条件的4个整数列表，如果无法生成则返回空列表
    """
    if upper_limit <= base:
        raise ValueError("上限必须大于基数")
    
    # 调整下限为大于base的最小整十数
    min_value = ((base // 10) + 1) * 10
    if min_value > upper_limit:
        return []  # 没有符合条件的整十数
    
    # 生成整十数列表
    eligible_numbers = [num for num in range(min_value, upper_limit+1, 10)]
    
    if len(eligible_numbers) < 4:
        return []  # 没有足够的整十数可供选择
    
    # 尝试多次生成符合条件的数值
    for _ in range(max_attempts):
        # 从符合条件的整十数中随机选择4个
        generated = random.sample(eligible_numbers, 4)
        
        # 计算每个生成的数值与基数的差
        diffs = [abs(num - base) for num in generated]
        
        # 检查所有三元组的差是否构成等差或等比数列
        valid = True
        for triplet in itertools.combinations(diffs, 3):
            # 检查原始顺序的三元组
            if is_arithmetic(triplet) or is_geometric(triplet):
                valid = False
                break
            # 检查排序后的三元组
            sorted_triplet = sorted(triplet)
            if is_arithmetic(sorted_triplet) or is_geometric(sorted_triplet):
                valid = False
                break
        
        if valid:
            return generated
    
    return []  # 如果尝试多次仍无法生成符合条件的数值

def main():
    try:
        base = int(input("请输入基数（整数）: "))
        upper_limit = int(input("请输入上限数值（整数）: "))
        
        if upper_limit <= base:
            print("错误: 上限必须大于基数")
            return
        
        numbers = generate_numbers(base, upper_limit)
        
        if numbers:
            print(f"\n生成的4个个位为0的整数（范围: {((base // 10) + 1) * 10} 到 {upper_limit}）:")
            for num in numbers:
                print(f"- {num} (与基数的差: {abs(num - base)})")
                
            # 验证输出，确保没有等差或等比数列
            diffs = [abs(num - base) for num in numbers]
            has_arith = False
            has_geom = False
            for triplet in itertools.combinations(diffs, 3):
                if is_arithmetic(triplet) or is_arithmetic(sorted(triplet)):
                    has_arith = True
                if is_geometric(triplet) or is_geometric(sorted(triplet)):
                    has_geom = True
            if has_arith:
                print("警告: 发现等差数列！")
            if has_geom:
                print("警告: 发现等比数列！")
        else:
            print(f"在{max_attempts}次尝试内无法生成符合条件的整数，请尝试调整基数或上限。")
    
    except ValueError as e:
        print(f"输入错误: {e}")
    except Exception as e:
        print(f"发生错误: {e}")

if __name__ == "__main__":
    main()    